package com.taiji.dicom4cloud.smart.camera;

import com.netsdk.demo.module.LoginModule;
import com.netsdk.lib.NetSDKLib;
import com.netsdk.lib.ToolKits;
import com.taiji.dicom4cloud.smart.handler.RegisterServiceCB;
import com.taiji.dicom4cloud.smart.service.SysConfigService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @ClassName AsyncTaskService
 * @Description 摄像头主动注册服务,方案三
 * @Author HX
 * @Date 2021/9/18 13:55
 **/
@Slf4j
@Component
@RequiredArgsConstructor
public class AutoRegisterService {

    @Autowired
    private SysConfigService sysConfigService;

    @Autowired
    private RegisterServiceCB registerServiceCB;

    // 监听服务句柄
    public static NetSDKLib.LLong mServerHandler = new NetSDKLib.LLong(0);

    //启动服务
//    @PostConstruct
    public void startRegsiterServer() {
        Map<String, Object> configMap = sysConfigService.getConfigByKeys("camera_server_address,camera_server_port");
        Integer port = Integer.parseInt(configMap.get("camera_server_port").toString());
        LoginModule.init(null,null);
        // 启动监听服务
        mServerHandler = LoginModule.netsdk.CLIENT_ListenServer((String)configMap.get("camera_server_address"), port, 1000,
                registerServiceCB, null);
        if (0 == mServerHandler.longValue()) {
            log.error("Failed to start server." + ToolKits.getErrorCodePrint());
        } else {
            log.info("Start server, [Server address %s][Server port %d]\n", "", port);
        }
    }

    //停止服务
    public void stopServer(){
        if(mServerHandler.longValue() != 0) {
            LoginModule.netsdk.CLIENT_StopListenServer(mServerHandler);
            mServerHandler.setValue(0);
            System.out.println("Stop server!");
        }
    }

}